﻿using UnityEngine;
using System.Collections;

public class PlatformDiagonalMovement : MonoBehaviour
{
	public float speed;
	public float distance;
	public bool switchMovement;
	public bool rotate;

	
	private Transform PlatformTransform;
	private bool up;
	private float start;
	private float end;
	
	void Start()
	{
		up = true;
		PlatformTransform = transform;
		start = rigidbody.position.y;
		end = rigidbody.position.y + distance;
	}
	
	void FixedUpdate()
	{
		if(rotate)
		{
			if(up)
			{
				// going up
				PlatformTransform.position += Vector3.up * speed;
				
				if(switchMovement)
				{
					PlatformTransform.position -= Vector3.forward * speed;
				}
				else
				{
					PlatformTransform.position += Vector3.forward * speed;
				}
				
				if(rigidbody.position.y >= end)
				{
					up = false;
				}
			}
			else 
			{
				// going down
				PlatformTransform.position -= Vector3.up * speed;
				
				if(switchMovement)
				{
					PlatformTransform.position += Vector3.forward * speed;
				}
				else
				{
					PlatformTransform.position -= Vector3.forward * speed;
				}
				
				if(rigidbody.position.y <= start)
				{
					up = true;
				}
			}
		}
		else
		{
			if(up)
			{
				// going up
				PlatformTransform.position += Vector3.up * speed;

				if(switchMovement)
				{
					PlatformTransform.position -= Vector3.right * speed;
				}
				else
				{
					PlatformTransform.position += Vector3.right * speed;
				}
				
				if(rigidbody.position.y >= end)
				{
					up = false;
				}
			}
			else 
			{
				// going down
				PlatformTransform.position -= Vector3.up * speed;

				if(switchMovement)
				{
					PlatformTransform.position += Vector3.right * speed;
				}
				else
				{
					PlatformTransform.position -= Vector3.right * speed;
				}
				
				if(rigidbody.position.y <= start)
				{
					up = true;
				}
			}
		}
	}
}